Skip to content

fix(ui): decode network images no larger than they are shown - #264

Open
xsahil03x wants to merge 13 commits into
mainfrom
fix/network-image-decode-size
Open

xsahil03x wants to merge 13 commits into
mainfrom
fix/network-image-decode-size

Conversation

@xsahil03x

@xsahil03x xsahil03x commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of FLU-1377

What

StreamNetworkImage, and so StreamAvatar, decoded every image at its full size, whatever size it was shown at. It now decodes images for the size they're shown at, like Flutter's ResizeImage and Coil:

  • Decode size: cacheWidth/cacheHeight when given, otherwise width/height × the device pixel ratio, fitted with the aspect ratio kept and never enlarged. Fits that fill the box (cover, fill, fitWidth, fitHeight) decode large enough to fill it, so non-square avatars stay sharp. Every image is capped at 4096 px on a side.
  • Size limit: an image with more pixels than 8192 × 8192 fails to load (except on web), read from its header before decoding.
  • StreamBoundedImage (internal) implements this on top of CachedNetworkImageProvider, modelled on ResizeImage. StreamNetworkImage renders a Flutter Image with it in place of the CachedNetworkImage widget, which can't take a custom provider. Loading, the shared disk cache, placeholders, the cache-hit check and retry behave as before. Like FadeInImage, it tracks loading from Image's own frameBuilder and errorBuilder, so Image alone listens to the image: loads still wait while a list is flung fast, and hidden animated images pause.
  • evictFromCache also removes every decoded size of the image from memory. The sizes it tracks are forgotten once they leave the image cache.
  • opacity, semanticLabel and excludeFromSemantics now apply; they were accepted but never reached the image. The gallery playground has an opacity knob.

Behaviour changes, in the CHANGELOG: cacheWidth with cacheHeight now fits instead of stretching, and images over the size limit show the error builder.

Verified

  • Unit tests for the decode sizes (fit, fill, never enlarging, the 4096 cap, logical sizes), the size limit, failed loads loading again, per-size caching and rebuild stability.
  • Widget tests against a local server: loading waits while a list is flung fast; a retry after the server recovers shows the image, both by tapping the retry badge and through retryListenable, and evictFromCache clears the decoded image.
  • Through a real StreamAvatar: a 6000×4000 photo decodes at 180×120, and a 14,500² image is refused with peak memory flat (about 240 MB, against about 1 GB before).
  • dart analyze --fatal-infos, dart format and check:barrels pass. The only local test failures are macOS golden variants, identical on main.

🤖 Generated with Claude Code

xsahil03x and others added 9 commits November 25, 2026 18:27
StreamNetworkImage, and so StreamAvatar, decoded every image at its full
size. It now reads the image's size first, refuses images larger than
8192 by 8192 pixels, and decodes the rest within the size it is shown at,
at most 4096 pixels on a side, keeping the aspect ratio. Given both
cacheWidth and cacheHeight, the image is fitted within them instead of
stretched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A failed load is removed from the image cache, so retrying fetches the
  image again instead of showing the same error.
- An image that covers its box, such as an avatar, is decoded large
  enough to fill it rather than to fit inside it.
- An image already on disk shows without the loading placeholder, as
  before.
- The size check runs as part of decoding, and the docs note it doesn't
  apply on web.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also decodes a BoxFit.none image at its own size, since it is drawn at
that size, and checks a rebuild keeps the same image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Takes a nullable width and height with useLogicalSize, like ResizeImage,
so the device pixel ratio comes from the image configuration rather than
from MediaQuery in build. Rounds down like ResizeImagePolicy.fit, and
fills the box for every fit that fills it, as Coil does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Returns the empty image itself, as OctoImage does, rather than a box of
the same size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The state listens to the image stream itself, as Image does, instead of
setting the error flag from errorBuilder, and every widget showing a
failed URL retries when the retry listenable notifies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
errorBuilder runs for each widget showing the error, including widgets
that share one image stream, so a separate stream listener isn't needed.
The flag is cleared when the URL or cache key changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Decoded images are cached under the bounded image's key, which
CachedNetworkImage.evictFromCache doesn't know, so evictFromCache left
them in memory and the old image kept showing. StreamBoundedImage now
remembers the keys it creates for each image, and evictFromCache evicts
them all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers tapping the retry badge and notifying the retry listenable for the
same URL, against a local server that fails until it recovers. Each test
has its own file because the image cache manager is shared app-wide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xsahil03x
xsahil03x requested a review from a team as a code owner November 26, 2026 14:30
@github-actions github-actions Bot added the ready Pull request ready to be reviewed label Nov 26, 2026
xsahil03x and others added 4 commits November 26, 2026 15:42
Replaces the separate error, placeholder, cache-check and retry flags
with one load state (checkingCache, loading, loaded, failed) and a
generation counter. Loads and errors come from an image stream listener,
since Image keeps its last error across a source change and kept setting
the old error flag again. Any change to the image or its decoded size now
restarts the state, so a failed image that loads at a new size no longer
reloads on a retry notification.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
opacity, semanticLabel and excludeFromSemantics were accepted but never
reached the image. The gallery playground gains an opacity knob.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e registry

The state's own image listener skipped Flutter's deferred loading while a
list is flung fast, kept animated images decoding while hidden, and never
released the image handles it was given. It is gone: like FadeInImage, the
state records loaded in frameBuilder and failed in errorBuilder, and Image
is remounted for each new source, so it carries no old error.

The registry of decoded sizes used by evictFromCache now forgets images no
longer in the image cache once it tracks more than the cache can hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready Pull request ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant